﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Core.User;
using Core.Util;

namespace Core.Features.Salutations.ViewModel
{
    public enum SalutationPage { Salutation = 0, GetUserInfo, }

	public class HelloDialogVM : INotifyPropertyChanged
	{
	    private string _askSentence = "Hi, my name is Sammy.  What's your name?";
	    private string _askSentenceConfirmation = "Got it.\n I'll remember that!";
        private SalutationPage _pageState = SalutationPage.Salutation;
        private readonly Dictionary<string, UIElement> _pages = new Dictionary<string, UIElement>();

	    public string HelloSentence
	    {
	        get
	        {
                return string.Format("{0}, {1}!", 
                        SalutationWords.GetRandomSalutation(), UserName);
	        }
	    }
	    public string UserName
	    {
            get { return UserProfileSession.GetSession().User.UserName; }
	        set
	        {
	            string name = "";
	            name = value.ToUpper();
	            if (name.Length > 1)
	                name = string.Format("{0}{1}", 
                        name[0],value.Substring(1));
	            UserProfileSession.GetSession().User.UserName = name;
                raisePropertyChanged("HelloSentence");
                raisePropertyChanged("UserName");
	        }
	    }
        public string AskSentence { get { return _askSentence; } }
        public string AskSentenceConfirmation { get { return _askSentenceConfirmation; } }
	    public SalutationPage PageState { get { return _pageState; } }

	    public event EventHandler DialogStateChanged = delegate { }; 

	    public ICommand UernameInfoCommand
	    {
	        get
	        {
	            return  new CommandHandler(o =>
	            {
                    raisePropertyChanged("UserName");
                    raisePropertyChanged("HelloSentence");
                    showPage("userInfoConfirmation");
	                var tmr = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(2224)};
	                tmr.Tick += (sender, args) =>
	                {
                        swithcToSalutationPage();
	                    tmr.Tick -= delegate { };
                        tmr.Stop();
                        UserProfileSession.SaveSession();
	                };
                    tmr.Start();
	            }, obj =>
	            {
	                if (obj is string)
	                    return (obj as string).Length > 0;
	                return false;
	            });
	        }
	    }


	    public event PropertyChangedEventHandler PropertyChanged = delegate { };

            private void raisePropertyChanged(string name)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }

	    public event EventHandler<SalutationPageChangeEventArgs> PageChanged = delegate { }; 

	    public HelloDialogVM(UIElement salutation, UIElement userInfo, UIElement userInfoConfirmation)
	    {
            _pages.Add("salutation", salutation);
            _pages.Add("userInfo", userInfo);
            _pages.Add("userInfoConfirmation", userInfoConfirmation);
	        greetOrAskName();
	    }

	        private void greetOrAskName()
	        {
	            var profile = UserProfileSession.GetSession();
                if (profile.User.UserName.Length == 0)
	            {
	                _pageState = SalutationPage.GetUserInfo;
	                showPage("userInfo");
	                triggerPageStateChange();
	            }
	            else
	            {
	                swithcToSalutationPage();
	            }
	        }

	            private void swithcToSalutationPage()
	            {
	                _pageState = SalutationPage.Salutation;
	                showPage("salutation");
	                triggerPageStateChange();
                    DialogStateChanged(this, new EventArgs());
	            }

	                private void triggerPageStateChange()
	                {
	                    raisePropertyChanged("PageState");
	                    PageChanged(this, new SalutationPageChangeEventArgs(_pageState));
	                }

	        private void showPage(string p)
            {
                var page = _pages[p];
                if (!ReferenceEquals(page, null))
                {
                    foreach (var item in _pages.Values)
                        item.Visibility = Visibility.Collapsed;
                    page.Visibility = Visibility.Visible;
                }
            }

	}

    public class SalutationPageChangeEventArgs : EventArgs
    {
        private readonly SalutationPage _state;
        
        public SalutationPage State { get { return _state; } }

        public SalutationPageChangeEventArgs(SalutationPage state)
        {
            _state = state;
        }
    }
}